<template>

  <!-- 登录页面容器 -->
  <div class="login-container">
    <!-- 顶部标题 -->
    <div class="Top">
      <p style="color: pink;">GiliGili教育</p>
    </div>

    <!-- Logo 容器 -->
    <div class="logo-container">
      <img src="@/assets/logo2.png" alt="Logo" class="logo">
    </div>

    <!-- 登录框 -->
    <div class="login-box">
      <!-- 登录标题 -->
      <h2 class="login-title">登录</h2>

      <!-- 账号输入框 -->
      <div class="form-group">
        <label for="username">账号</label>
        <input v-model="username" id="username" type="text" placeholder="请输入您的账号">
      </div>

      <!-- 密码输入框 -->
      <div class="form-group">
        <label for="password">密码</label>
        <input v-model="password" id="password" type="password" placeholder="请输入您的密码">
      </div>

      <!-- 登录按钮 -->
      <button class="btn-login" @click="login">登录</button>

      <!-- 登录帮助信息 -->
      <div class="login-help">
        <!-- 错误提示 -->
        <p v-if="error" class="error-message">{{ error }}</p>

        <br>
        <!-- 找回密码链接 -->
        <router-link to="/FindPwd">忘记密码，点击找回?</router-link>
        <br><br>
        <!-- 注册链接 -->
        <router-link to="/RegisterView">没有账号，点击注册?</router-link>
      </div>
    </div>
  </div>

</template>

<script>
import axios from 'axios';
export default {
  data() {
    return {
      username: '',
      password: '',
      error: null
    };
  },
  methods: {
    // 登录方法
    login() {
      this.error = null;
      // 发送登录请求
      axios.post('http://localhost:8081/springboot01/user/login',
          {
            username: this.username,
            password: this.password
          },
          {
            headers: {
              'Content-Type': 'application/json'
            }
          }
      )
          .then(response => {
            // 登录成功处理
            console.log('Response data:', response.data);
            if (response.data.msg === "请求成功!") {
              // 将用户信息存储到本地存储中
              localStorage.setItem("user", JSON.stringify(response.data.data))
              // 判断用户权限，跳转到不同页面
              if (response.data.data.admin) {
                // 如果用户是管理员，则跳转到管理员首页
                this.$router.replace('/MHomeView');
              } else {
                // 如果用户不是管理员，则跳转到普通用户首页
                this.$router.replace('/HomeView');
              }
            } else {
              // 处理登录失败情况
              alert('登录失败，请检查账号和密码');
            }
          })
          .catch(error => {
            console.error('登录失败:', error);
            // 显示错误消息
            this.error = '登录失败，请稍后重试'; 
          });
      }
  }
};
</script>

<style scoped>
.Top {
  position: absolute; 
  top: 0; 
  margin-top:50px;
  font-size: 48px;
  background-image: linear-gradient(45deg, #ff269b, #2ab5f5, #ffbf00);
    mask: radial-gradient(circle at 50% 0%, #f107be, transparent 50%);
    animation: scale 6s infinite;
}
@keyframes scale {
    0% {
        mask-size: 100% 100%;
    }
    60%,
    100% {
        mask-size: 150% 800%;
    }
}


.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;

  background-color: #ffffff;
}

.login-box {
  width: 350px;
  height: 450px;
  background-color: #ffffff;
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);

}

.login-title {
  font-size: 24px;
  margin-bottom: 20px;
  text-align: center;
}

.form-group {
  margin-bottom: 20px;
}

label {
  font-weight: bold;
}

input {
  width: 100%;
  padding: 10px;
  font-size: 16px;
  border: 1px solid #ccc;
  border-radius: 4px;
}


body { 
   display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  font-size: 10px;
  background-color: #333333; 


} 

.btn-login {
  width: 100%;
  height: 2.25rem;
  background-color: #ff9bad;
  color: white;
  border: none;
  border-radius: 3rem;
  font-size: 0.8em;
  font-weight: 700;
  cursor: pointer;
  transition: all .2s;
  position: relative;
  z-index: 1;
  overflow: hidden;
}

.btn-login:hover {
  color: #ff9bad;
}
.btn-login:after {
  content: "";
  background: white;
  position: absolute;
  z-index: -1;
  left: -20%;
  right: -20%;
  top: 0;
  bottom: 0;
  transform: skew(-45deg) scale(0, 1);
  transition: all 0.5s;
}
.btn-login:hover:after {
  transform: skew(-45deg) scale(1, 1);
}





.login-help {
  margin-top: 10px;
  text-align: center;
}

.login-help a {
  color: #007bff;
  text-decoration: none;
}

.login-help a:hover {
  text-decoration: underline;
}

.error-message {
  color: red;
  margin-top: 10px;
  text-align: center;
}



</style>
